
import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";
import { SDKDir, SDKLogicState, SDKState } from "../../SDKDefine";
/**
 * https://developers.weixin.qq.com/minigame/dev/guide/open-ability/ad/banner-ad.html
 * 创建 banner 广告组件。请通过 getSystemInfoSync() 返回对象的 SDKVersion 
 * 判断基础库版本号 >= 2.0.4 后再使用该 API。每次调用该方法创建 banner 广告都会返回一个全新的实例。
 */

@registerClass('WXBannerAd')
export default class WXBannerAd extends BaseAd {

    init(): void {
        this.preload();
    }

    open(): void {
        this.setLogicState(SDKLogicState.open)
        if (this.isState(SDKState.loadSucess)) {
            this.show()
        } else {
            this.create();
        }
    }

    close(): void {
        this.setState(SDKState.close)
        if (this.ad) {
            this.ad.hide();
        }
    }


    protected onLoadLogic(): void {
        console.log('onLoadLogic this.isLogicState(SDKLogicState.open) ', this.isLogicState(SDKLogicState.open))
        if (this.isLogicState(SDKLogicState.open)) {
            this.show()
        }
    }

    show(): void {
        this.setState(SDKState.show)
        this.setLogicState(SDKLogicState.close)
        if (this.ad) {
            console.log(' wx banner ad show ', this.adUnitID)
            this.ad.show()
                .catch(err => console.log('show', err))
        }
    }

    getBannerStyle(): any {
        let winSize = this.sdk.getSystemInfoSync();
        let x = 0
        let y = 0
        switch (this.param.dir) {
            case SDKDir.BOTTOM_MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = winSize.windowHeight - this.style.height;
                break;
            case SDKDir.UP_MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = 0
                break;
            case SDKDir.MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = (winSize.windowHeight - this.style.height) / 2;
                break;
            case SDKDir.RIGHT_MID:
                x = (winSize.windowWidth - this.style.width)
                y = (winSize.windowHeight - this.style.height) / 2;
                break;
            case SDKDir.LEFT_TOP:
                x = 0
                y = 0
                break;
            case SDKDir.BOTTOM_LEFT:
                x = 0
                y = winSize.windowHeight - this.style.height;
                break;
            case SDKDir.XY:
                x = this.style.left//this.rx * winSize.windowWidth;
                y = this.style.top//this.ry * winSize.windowHeight;
                break;
            case SDKDir.WHITE:
                x = -this.style.width + 1
                y = -this.style.height + 1

                break;
        }
        return {
            left: x,
            top: y,
            width: this.style.width,
            height: this.style.height,
        }
    }

    updateSizeStyle(style: any) {
        if (!style) {
            return;
        }
        let winSize = this.sdk.getSystemInfoSync();
        console.log('updateSize', this.style.width, this.style.height, this.ad)
        if (this.style.width != 0 && this.style.height != 0 && this.ad) {
            switch (this.param.dir) {
                case SDKDir.BOTTOM_LEFT:
                    style.top = (winSize.windowHeight - this.style.height);
                    style.left = 0;
                    break;
                case SDKDir.BOTTOM_MID:
                    style.top = (winSize.windowHeight - this.style.height);
                    style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.RIGHT_MID:
                    style.top = (winSize.windowHeight - this.style.height) / 2;
                    style.left = winSize.windowWidth - this.style.width;
                    break;
                case SDKDir.MID:
                    style.top = (winSize.windowHeight - this.style.height) / 2;
                    style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.TOP_MID:
                    style.top = 0;
                    style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.XY:
                    style.top = this.style.top// this.ry * winSize.windowHeight;
                    style.left = this.style.left// this.rx * winSize.windowWidth;
                    break;
            }

        }
    }
    create(): void {
        this.destroy();
        this.setState(SDKState.loading)
        if (!this.ad) {
            let style = this.getBannerStyle();
            console.log('WXBannerAd create  this.adUnitID', this.adUnitID, style)
            this.ad = this.sdk.createBannerAd({
                adUnitId: this.adUnitID,
                adIntervals: 30,
                style: style
            })
            this.ad.onLoad(this.getFunc(this.onLoad))
            this.ad.onError(this.getFunc(this.onError))
            this.ad.onResize(this.getFunc(this.onResize))
        }
    }

    onResize(data: { width: number, height: number }): void {
        console.log('banner onResize', data)
        this.style.width = data.width;
        this.style.height = data.height;
        if (this.ad) {
            this.updateSizeStyle(this.ad.style)
        }

    }

    destroy(): void {
        if (this.ad) {
            console.warn(' banner destroy', this.ad)
            this.ad.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            this.ad.offResize(this.getFunc(this.onResize))
            this.ad.destroy();
            this.ad = null;
        }
    }
}
